package com.woniu.p2p.controller;

import com.woniu.p2p.service.P2PUserService;
import com.woniu.p2p.vo.ZhifubaoChargeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author 86152
 * @create 2020/8/8
 * @since 1.0.0
 */
@RestController
@RequestMapping("/charge")
public class P2PChargeController {
    @Autowired
    P2PUserService p2PUserService;

    /*支付宝充值*/
    @RequestMapping("/zhifubaocharge")
    public void charge(@RequestBody ZhifubaoChargeVo zhifubaoChargeVo, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderId = UUID.randomUUID().toString(); // 订单号：需要自己生成（时间戳+商品id+用户id）
        String orderAmount = zhifubaoChargeVo.getOrderAmount()+"";
        String orderSubject = zhifubaoChargeVo.getOrderSubject();
        String orderDescription = zhifubaoChargeVo.getOrderDescription();

        String token=request.getHeader("token");
        int userid = Integer.parseInt(token);
        String chargeType="支付宝";
        String chargeStatus="已充值";
        String chargeContext="支付宝充值";
        p2PUserService.charge(orderId,new Date(),orderAmount,chargeType,chargeStatus,chargeContext,userid);

        AlipayUtil.payment(request,response,orderId,orderAmount,orderSubject,orderDescription);
    }
}
